const express = require('express');
const cors = require('cors');
let OSS = require('ali-oss');

const {
    response
} = require('express');
const {
    Socket
} = require('dgram');

//
const census = require('./middleware/census')

const app = express();

// 设置跨域许可
app.use(cors())

//生成token
app.set('token', 'asjfhjsakfs8348dasdsdgtsdhjagjsadfhsjfsfhfhfshg3942843204');

//静态文件托管
app.use('/admin', express.static('./admin'));
app.use('/uploads', express.static('./uploads'));
app.use('/clothing', express.static('./bootstrap'));
app.use('/film', express.static('./jquery'));

// 处理 post 请求数据
app.use(express.json())
app.use(express.urlencoded({
    extended: false
}))

//连接数据库
require('./models/db')

//后台接口
app.use('/admin/api', require('./router/admin/index'))


//前端接口
app.use('/web/api', require('./router/web/index'))


let client = new OSS({
    region: 'oss-cn-shenzhen', //阿里云对象存储地域名
    accessKeyId: 'LTAI5tG3pchVxj6pjqEpXuRe', //api接口id
    accessKeySecret: 'WnweDoDQAcfFyOMbrAkgzFpUdjxuXz', //api接口密码
    bucket: 'baigei'
})

// 删除oss文件
app.get('/aaa', async (req, res, next) => {
    try {
        let result = await client.delete('46b1e5c51205b4df3d9b92066ff25d5f.webp');
        console.log(result) //在此处记录 url name 等信息
        res.send(result)
    } catch (err) {
        console.log(err)
    }
})

// 查找oss文件
app.get('/bbb', async (req, res, next) => {
    try {
        // res.send(111)
        let result = await client.head('46b1e5c51205b4df3d9b92066ff25d5f.webp');
        console.log(result) //在此处记录 url name 等信息
        res.send(result)
    } catch (err) {
        console.log(err)
    }
})

// 每个月1日00:00:00执行统计
census()


const PORT = 3000

app.listen(PORT, () => {
    console.log(`启动成功,端口号:${PORT}`);
})